package com.wx.wxapp.service;

import com.wx.wxapp.dao.DiseaseMapper;
import com.wx.wxapp.dao.FoodMapper;
import com.wx.wxapp.entity.Disease;
import com.wx.wxapp.entity.Food;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PraiseService {

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    public void PraiseFoodCard(String cardId){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        FoodMapper foodMapper = sqlSession.getMapper(FoodMapper.class);
        Food food = foodMapper.selectByPrimaryKey(cardId);
        int praNum = food.getPraisenumber();
        praNum += 1;
        food.setPraisenumber(praNum);
        foodMapper.updateByPrimaryKey(food);
        sqlSession.close();
    }

    public void CancelPraiseFoodCard(String cardId){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        FoodMapper foodMapper = sqlSession.getMapper(FoodMapper.class);
        Food food = foodMapper.selectByPrimaryKey(cardId);
        int praNum = food.getPraisenumber();
        praNum -= 1;
        food.setPraisenumber(praNum);
        foodMapper.updateByPrimaryKey(food);
        sqlSession.close();
    }

    public void PraiseDiseaseCard(String cardId){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        DiseaseMapper diseaseMapper = sqlSession.getMapper(DiseaseMapper.class);
        Disease disease = diseaseMapper.selectByPrimaryKey(cardId);
        int praNum = disease.getPraisenumber();
        praNum += 1;
        disease.setPraisenumber(praNum);
        diseaseMapper.updateByPrimaryKey(disease);
        sqlSession.close();

    }

    public void CancelPraiseDiseaseCard(String cardId){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        DiseaseMapper diseaseMapper = sqlSession.getMapper(DiseaseMapper.class);
        Disease disease = diseaseMapper.selectByPrimaryKey(cardId);
        int praNum = disease.getPraisenumber();
        praNum -= 1;
        disease.setPraisenumber(praNum);
        diseaseMapper.updateByPrimaryKey(disease);
        sqlSession.close();
    }



}
